package top.bug.blog.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.bug.blog.annotation.Auth;
import top.bug.blog.entity.dto.common.BatchRequest;
import top.bug.blog.common.Result;
import top.bug.blog.entity.dto.user.*;
import top.bug.blog.entity.vo.user.LoginUserVO;
import top.bug.blog.entity.vo.user.SafeUserVO;
import top.bug.blog.enums.ErrorCode;
import top.bug.blog.exception.BussinessException;
import top.bug.blog.service.UserService;
import top.bug.blog.utils.StrUtils;

import javax.annotation.Resource;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 后台-用户模块
 */
@Api(tags = "用户管理模块")
@RestController
@RequestMapping("/userApi")
@Auth
public class UserController {
    @Resource
    private UserService userService;

    // region 登录相关

    @Auth(isCheck = false)
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "需提供用户名和密码")
    public Result<LoginUserVO> login(@RequestBody UserLoginRequest loginRequest) {
        if (loginRequest == null) {
            throw new BussinessException(ErrorCode.PARAM_NOT_COMPLETE);
        }
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        if (StrUtils.isAnyBlank(username, password)) {
            throw new BussinessException(ErrorCode.PARAM_NOT_COMPLETE);
        }
        return Result.success(userService.userLogin(username, password));
    }

    @PostMapping("/logout")
    @ApiOperation("用户注销")
    public Result<Boolean> logout() {
        return Result.success(userService.logout());
    }

    // endregion


    // region 增删改查

    @GetMapping("/getCurrentInfo")
    @ApiOperation("获取当前登录用户信息")
    public Result<LoginUserVO> getCurrentInfo() {
        return Result.success(userService.getCurrentUserInfo());
    }

    @PostMapping("/getUserList")
    @ApiOperation("获取用户列表")
    public Result<Page<SafeUserVO>> getUserList(@RequestBody UserQueryRequest queryRequest) {
        return Result.success(userService.getUserList(queryRequest));
    }

    @PostMapping("/add")
    @ApiOperation("新增用户")
    @Auth(isCheck = false)
    public Result<Boolean> add(@RequestBody UserAddRequest addRequest) {
        return Result.success(userService.add(addRequest));
    }

    @PostMapping("/edit")
    @ApiOperation("更新用户")
    public Result<Boolean> edit(@RequestBody UserUpdateRequest updateRequest) {
        return Result.success(userService.edit(updateRequest));
    }

    @PostMapping("/resetPwd")
    @ApiOperation("重置密码")
    public Result<Boolean> resetPwd(@RequestBody UserResetPwdRequest resetPwdRequest) {
        return Result.success(userService.resetPwd(resetPwdRequest));
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    public Result<Boolean> delete(@RequestBody BatchRequest batchRequest) {
        return Result.success(userService.delete(batchRequest));
    }

    // endregion
}